<?php


namespace app\common\model;

use think\facade\Session;
use app\common\model\Common as CommonModel;

class News extends CommonModel
{
    protected $pk = 'id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 10;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);


        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        $re['page'] = $page;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];

        if(isset($post['cate_id']) && !empty($post['cate_id'])){
            $where[] = ['cate_id','=',$post['cate_id']];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = ['orderid'=>'asc',$pk => 'desc'];
        return $result;
    }

    protected function tableFormat($list)
    {
        $Classify = new Classify();
        foreach($list as $k=>$v){
            $list[$k]['add_time'] = date("Y-m-d",$v['add_time']);
            $c_one = $Classify->where("id","=",$v['cate_id'])->find();
            $list[$k]['cate_name'] = $c_one['name'];
        }
        return $list;
    }

    public function handle($param){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        $act = $param['act'];
        //$param['userid'] = isset($param['user_id'])? $param['user_id'] : cookie('user_id');
        if(isset($param['token'])){
            unset($param['token']);
        }
        if($act == 'add'){
            unset($param['act']);
            $param['add_time'] = isset($param['add_time']) ? strtotime($param['add_time']) : time();
            $param['orderid'] = GetOrderID("house");
            $r = $this->save($param);
            $id = $this->id;
        }
        if($act == 'edit'){
            $param['update_time'] = time();
            $id = $param['id'];
            unset($param['act']);
            unset($param['id']);
            $r = $this->where('id','=',$id)->update($param);
        }
        if(!$r){
            $result['msg'] = '操作失败';
            return $result;
        }
        $result['code'] = 1;
        $result['msg'] = '操作成功';
        $result['data'] = ['id'=>$id];
        return $result;
    }

    public function info($id){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        $info = $this->where("id","=",$id)->find();
        if(!isset($info['id'])){
            $result['msg'] = "信息不存在";
        }
        $Classify = new Classify();
        $info['add_time'] = date("Y-m-d",$info['add_time']);
        $c_one = $Classify->where("id","=",$info['cate_id'])->find();
        $info['cate_name'] = $c_one['name'];

        //富文本替换图片src
        $domain = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME');
        $newHtml = addDomainToImgSrc($info['content'], $domain);
        $info['content'] = $newHtml;

        $result['code'] = 1;
        $result['msg'] = "获取成功";
        $result['data'] = $info;
        return $result;
    }

    public function deleted($id){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        $res = $this->where("id","=",$id)->delete();
        if(!$res){
            $result['msg'] = "删除失败";
        }
        $result['code'] = 1;
        $result['msg'] = "删除成功";
        return $result;
    }

}